<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>LC设备列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${hostUrl}/static/layui/css/layui.css"  media="all">
    <script src="${hostUrl}/static/layui/layui.js"></script>
    <script src="${hostUrl}/static/layui/custom/js/dickov.js"></script>
    <script>
        layui.use(['layer','element'], function(){
            // 使用弹出层
            var layer = layui.layer;
            <#if devList?? && (devList?size > 0) >
                layer.msg('欢迎查看设备列表');
            <#else>
                layer.msg('当前没有设备在线');
            </#if>
            // 使用进度条
            var element = layui.element;
        });
    </script>
</head>
<body>
<table class="layui-table">
    <colgroup>
        <col width="50">
        <col width="300">
        <col width="180">
        <col width="140">
        <col width="300">
    </colgroup>
    <thead>
    <tr>
        <th>序号</th>
        <th>设备名</th>
        <th>更改配置</th>
        <th>固件升级</th>
        <th>升级进度</th>
    </tr>
    </thead>
    <tbody>
    <#list devList as dev>
        <tr>
            <td>${dev_index + 1}</td>
            <td>
                ${dev.deviceName}(${dev.deviceId})
                <button class="layui-btn layui-btn-sm layui-btn-primary"  onclick="showDevInfo( '${dev.deviceId}' )"> 详情 </button>
            </td>
            <td>
                <a class="layui-btn layui-btn-sm layui-btn-radius" href="hsPage?devId=${dev.deviceId}"> 硬件配置 </a>
                <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" href="lcPage?devId=${dev.deviceId}"> 灯控配置 </a>
                <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" href="ccPage?devId=${dev.deviceId}"> 中控配置 </a>
            </td>
            <td>
                <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="requireUpdateFirmware('${dev.deviceId}','${dev.deviceName}')"> 固件升级 </button>
                <button class="layui-btn layui-btn-sm layui-btn-primary " onclick="showProgress('${dev.deviceId}','${dev.deviceName}')"> 刷新进度 </button>
            </td>
            <td>
                <div class="layui-progress" lay-filter="proBar_${dev.deviceId}" >
                    <div class="layui-progress-bar" lay-percent="0%" id="p_${dev.deviceId}" ></div>
                </div>
            </td>
        </tr>
    </#list>
    </tbody>
</table>
</body>
<script>

    function showDevInfo ( devId ){
        // layui内置jquery，但因为规范，不能直接用$来取出jquery，故需重新定义$;
        // console.log(layui.$.fn.jquery);// 此语句用于打印内置jquery的版本号
        var $ = layui.$;
        $.ajax({
            url:"${hostUrl}/device/info",
            data:{'devId':devId},
            type:"post",
            dataType:"json",
            success:function(devJson){
                var str = '设备ID ：' + devJson.deviceId
                    + '<br/>设备名称：'  + devJson.deviceName
                    + '<br/>固件版本：' + devJson.deviceFirmwareVersion
                    + '<br/>是否AI版本：' + (devJson.deviceAddress == 1 ? '是':'否')
                    + '<br/>上线时间：' + formatDate(devJson.deviceLastConnectedTime)
                layer.alert( str ,
                    {   icon: 1 ,
                        title:'设备信息'}
                    );
            },
            error:function(data){
                layer.msg("此设备已离线，请重新刷新本页面");
            }
        });
    }

    function requireUpdateFirmware( devId,devName){

        var $ = layui.$;
        var element = layui.element;
        var layer = layui.layer;

        $.ajax({
            url:"/lc/device/requireUpdateFirmware",
            data:{'devId':devId},
            type:"get",
            dataType:"json",
            success:function(response){
                layer.msg("设备("+ devName + ")" + response.msg);
                if(response.code != -2){
                    showProgress(devId,devName);
                }
            },
            error:function(response){
                layer.msg("错误"+response.msg);
            }
        });
    }

    function requireUpdateHardwareSet( devId,devName){

        var $ = layui.$;
        var element = layui.element;
        var layer = layui.layer;

        $.ajax({
            url:"/lc/device/requireUpdateHardwareSet",
            data:{'devId':devId},
            type:"get",
            dataType:"json",
            success:function(response){
                layer.msg("设备("+ devName + ")" + response.msg);
            },
            error:function(response){
                layer.msg("错误"+response.msg);
            }
        });
    }

    function showProgress( devId,devName){

        var $ = layui.$;
        var element = layui.element;

        $.ajax({
            url:"${hostUrl}/device/updateProgress",
            data:{'devId':devId},
            type:"get",
            dataType:"json",
            success:function(response){
                var proPercent = response.data ;
                if( proPercent < 0 || proPercent >= 100 ){
                    layer.alert( '设备('+devName+')' + response.msg,
                        {   icon: 1 ,
                            title:'升级信息'}
                    );
                    element.progress('proBar_'+devId , 0+'%');
                }
                else{
                    element.progress('proBar_'+devId , proPercent+'%');
                    sleep(400);
                    showProgress(devId, devName);
                }
            },
            error:function(response){
                $.messager.alert('错误',response.msg);
            }
        });
    }

</script>


</html>